package com.pengheng.controller;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @remark
 * @Author pengheng
 * @date 2021/4/23 14:37
 */
@RestController
@RequestMapping("/distributed")
public class RedisDistributedLockController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/hello")
    public String hello() {
        RLock lock = redissonClient.getLock("my-lock");
        System.out.println("等待获取锁" + Thread.currentThread().getName());
        lock.lock();
        try {
            System.out.println("加锁成功，执行业务" + Thread.currentThread().getName());
            Thread.sleep(15000);
        } catch (Exception e) {
        } finally {
            System.out.println("释放锁。。。" + Thread.currentThread().getName());
            lock.unlock();
        }
        return "hello world";
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/write")
    public String writeValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock writeLock = readWriteLock.writeLock();

        String s = "";
        try {
            writeLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(3000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    public String readValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock readLock = readWriteLock.readLock();
        readLock.lock();
        String s = null;
        try {
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
        return s;
    }

    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
//        park.acquire();//获得信号量
//        boolean b = park.tryAcquire();
        boolean b = park.tryAcquire(10, TimeUnit.SECONDS); //带时间的获取，超过时间没获取到就为false
        return "ok->" + b;
    }

    @GetMapping("/go")
    public String go() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();//释放信号量

        return "ok";
    }

    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);//设置数量
        door.await();//等待闭锁都完成
        return "放假了";
    }

    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id) throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();

        return id + "班的人走了";
    }
}
